package com.bawei.recyclerview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Author : yaotianxue
 * @Time : On 2023/9/13 13:37
 * @Description : UserAdapter
 */
//1:自定义类继承RecyclerView.Adapter
//3：泛型添加对应的ViewHolder并重写方法
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {
    private List<User> mList;

    public UserAdapter(List<User> list) {
        mList = list;
    }
    //创建viewholder
    @NonNull
    @Override
    public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new UserViewHolder(view);
    }
    //绑定数据
    @Override
    public void onBindViewHolder(@NonNull UserViewHolder holder, int position) {
        holder.tvUsername.setText(mList.get(position).getUsername()+"");
        holder.tvCaption.setText(mList.get(position).getCaption()+"");

        Glide.with(holder.mImageView)
                .load(mList.get(position).getImageUrl())
                .transform(new CenterCrop())
                .into(holder.mImageView);

    }
    //返回个数
    @Override
    public int getItemCount() {
        return mList.size();
    }

    //2：自定义Viewholder继承RecyclerView.ViewHolder
    class UserViewHolder extends RecyclerView.ViewHolder{
        private ImageView mImageView;
        private TextView tvUsername,tvCaption;

        public UserViewHolder(@NonNull View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.iv);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvCaption = itemView.findViewById(R.id.tv_caption);
        }
    }
}
